package com.xzsoft.gdwz.common;

import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.xzsoft.gdwz.dao.UserOrgMsgDao;
import com.xzsoft.gdwz.dao.ValueDao;
import com.xzsoft.gdwz.model.OrderHeader;
import com.xzsoft.gdwz.model.OrderLine;
import com.xzsoft.gdwz.model.UserOrgMsg;
import com.xzsoft.gdwz.model.ValueBean;

@Component
public class CommonUtil {
	@Autowired
	private UserOrgMsgDao userOrgMsgDao;
	
	@Autowired
	private ValueDao valueDao;
	
	/**
	 * 获取用户的组织信息
	 * @param userName
	 * @return
	 */
	@Cacheable(value="userOrgmsg",key="#userName")
	public UserOrgMsg getUserOrgMsg(String userName,String emName){
		return userOrgMsgDao.getUserOrgMsg(userName,emName);
	}
	
	/**
	 * 设置用户的组织信息
	 * @param oh
	 * @return
	 * @throws Exception 
	 */
	public OrderHeader initUserOrgMsg(OrderHeader oh) throws Exception{
		UserOrgMsg userOrgMsg = getUserOrgMsg(oh.getCreateBy(),oh.getEcommerceName());
		if(userOrgMsg == null){
			throw new  Exception(oh.getCreateBy()+"-用户信息不存在！");
		}
		oh.setEcommerceUserName(oh.getCreateBy());
		oh.setCreateBy(userOrgMsg.getUserId());
		oh.setComId(userOrgMsg.getComId());
		oh.setComName(userOrgMsg.getComName());
		oh.setOrgId1(userOrgMsg.getOrgId1());
		oh.setOrgId2(userOrgMsg.getOrgId2());
		oh.setOrgId3(userOrgMsg.getOrgId3());
		oh.setOrgId4(userOrgMsg.getOrgId4());
		oh.setOrgName1(userOrgMsg.getOrgName1());
		oh.setOrgName2(userOrgMsg.getOrgName2());
		oh.setOrgName3(userOrgMsg.getOrgName3());
		oh.setOrgName4(userOrgMsg.getOrgName4());
		oh.setOrgApp1(userOrgMsg.getOrgApp1());
		oh.setOrgApp2(userOrgMsg.getOrgApp2());
		oh.setOrgApp3(userOrgMsg.getOrgApp3());
		oh.setOrgApp4(userOrgMsg.getOrgApp4());
		oh.setInqHName(userOrgMsg.getOrgShortName());
		oh.setUserId(userOrgMsg.getUserId());
		oh.setUserName(userOrgMsg.getUserName());
		if(oh.getReqDate() == null){
			DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
			DateTime dateTime = DateTime.parse(oh.getCreateDate(), format);  
			String reqDate = dateTime.plusDays(5).toString("yyyy-MM-dd HH:mm:ss");
			oh.setReqDate(reqDate);
		}
		return oh;
	}
	
	public OrderLine initUserOrgMsg(OrderLine ol) throws Exception{
		UserOrgMsg userOrgMsg = getUserOrgMsg(ol.getCreatedBy(),ol.getEcommerceName());
		if(userOrgMsg == null){
			throw new Exception(ol.getCreatedBy()+"-用户信息不存在！");
		}
		ol.setCreatedBy(userOrgMsg.getUserId());
		ol.setComId(userOrgMsg.getComId());
		ol.setComName(userOrgMsg.getComName());
		ol.setOrgId1(userOrgMsg.getOrgId1());
		ol.setOrgId2(userOrgMsg.getOrgId2());
		ol.setOrgId3(userOrgMsg.getOrgId3());
		ol.setOrgId4(userOrgMsg.getOrgId4());
		ol.setOrgName1(userOrgMsg.getOrgName1());
		ol.setOrgName2(userOrgMsg.getOrgName2());
		ol.setOrgName3(userOrgMsg.getOrgName3());
		ol.setOrgName4(userOrgMsg.getOrgName4());
		ol.setOrgApp1(userOrgMsg.getOrgApp1());
		ol.setOrgApp2(userOrgMsg.getOrgApp2());
		ol.setOrgApp3(userOrgMsg.getOrgApp3());
		ol.setOrgApp4(userOrgMsg.getOrgApp4());
		ol.setUserId(userOrgMsg.getUserId());
		if(ol.getReqDate() == null){
			DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
			DateTime dateTime = DateTime.parse(ol.getCreationDate(), format);  
			String reqDate = dateTime.plusDays(5).toString("yyyy-MM-dd HH:mm:ss");
			ol.setReqDate(reqDate);
		}
		return ol;
	}
	
	@Cacheable(value="default",key="#userName")
	public List<ValueBean> getValSetList(String valSetCode){
		return valueDao.selectValuesBySetCode(valSetCode);
	}
	
	public static void main(String[] args){
		DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
		DateTime dateTime = DateTime.parse("2016-08-30 17:06:43", format);  
		System.out.println(dateTime.plusDays(5).toString("yyyy-MM-dd HH:mm:ss"));
	}
}
